import { defineStore } from 'pinia';
import { store } from '@/store';
import type { DeviceInfo } from '@/api/deviceinfo/deviceinfo';
import { getDeviceInfoApi } from '@/api/deviceinfo/deviceinfo';

export const useDeviceInfoStore = defineStore({
  id: 'app-device-info',
  state: (): DeviceInfo => {
    return {
      bootSystems: [],
      chipLevel: 0,
      defaultVersion: '',
      deviceSn: '',
      deviceType: '',
      enduserName: '',
      freqMax: 0,
      freqMin: 0,
      freqOffsetRatio: 0,
      licenseinfo: {
        antiInterferenceLevel: 0,
        dataEncryptionAES256: false,
        deviceList: [],
        extendSpan: false,
        freqSmart: false,
        maxHopNum: 0,
        maxNodeNum: 0,
        maxThroughput: 0,
        supportedWaveform: 0,
        time: '',
      },
      powerMax: 0,
      powerMaxAtten: 0,
      powerMaxAtten2: 0,
      supplementaryFreqBands: [],
      swapRxIQ: false,
      swapTxIQ: false,
      userName: '',
      version: '',
      wifiChannels: [],
    };
  },
  getters: {
    getPowerMax(): number {
      return this.powerMax;
    },
    getPowerMaxAtten(): number {
      return this.powerMaxAtten;
    },
    getPowerMaxAtten2(): number {
      return this.powerMaxAtten2;
    },
    getFreqMin(): number {
      return this.freqMin;
    },
    getFreqMax(): number {
      return this.freqMax;
    },
    getDeviceInfo: (state) => {
      console.log(state.powerMaxAtten);
      return { ...state };
    },
    getDeviceType: (state) => {
      return state.deviceType;
    },
    getVersion(): string {
      return this.version;
    },
    getMaxHopNum(): number {
      return this.licenseinfo.maxHopNum;
    },
    getMaxNodeNum(): number {
      return this.licenseinfo.maxNodeNum;
    },
  },
  actions: {
    setDeviceInfo(deviceinfo: DeviceInfo) {
      this.$patch(deviceinfo);
      //Object.assign(this.$state, deviceinfo);
    },
    // 获取设备信息
    async getInfo() {
      const data = await getDeviceInfoApi();
      const result = { ...data };

      this.setDeviceInfo(result);

      return result;
    },
  },
});

export function useDeviceInfo() {
  return useDeviceInfoStore(store);
}
